﻿Imports DTO
Imports BUS
Imports System.Data.OleDb
Public Class TiepNhanNhanVien


    Public Sub LoadData()
        Dim strSQL As String = "SELECT  * FROM NhanVien,BangCap,ChucVu,BoPhan where NhanVien.MaBangCap=BangCap.MaBangCap and NhanVien.MaBoPhan=BoPhan.MaBoPhan and NhanVien.MaChucVu=ChucVu.MaChucVu"
        Dim dataset As DataTable = New DataTable()

        Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=QUANLYTHUVIEN.accdb")
        Dim cmd As OleDbCommand = New OleDbCommand(strSQL, con)
        Dim ad As OleDbDataAdapter = New OleDbDataAdapter(cmd)
        ad.Fill(dataset)
        DataGridView_hosonhanvien.AutoGenerateColumns = False
        DataGridView_hosonhanvien.DataSource = dataset


        'Dim ds As List(Of NhanVienDTO)
        'Dim bus As New NhanVienBUS()
        'ds = bus.LayNhanVien()
        'DataGridView_hosonhanvien.Rows.Clear()
        'For i As Integer = 0 To ds.Count - 1
        '    DataGridView_hosonhanvien.Rows.Add(ds(i).MaNV, ds(i).HoTen, ds(i).DienThoai, ds(i).DiaChi, ds(i).NgaySinh, ds(i).MaBoPhan, ds(i).MaBangCap, ds(i).MaChucVu)
        'Next
       
    End Sub
    Private Sub TiepNhanNhanVien_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'load BangCap Len combox'''''''''''''''''''''''''
        Dim bangcap As New BangCapBUS()
        Dim dsach As New List(Of BangCapDTO)
        dsach = bangcap.laybangcap()
        ComboBox_bangcap.DataSource = dsach
        ComboBox_bangcap.DisplayMember = "TenBangCap"
        ComboBox_bangcap.ValueMember = "MaBangCap"

        ''''''''load CHucVu len combox''''''''''''
        Dim chucvu As New ChucVuBUS()
        Dim ds1 As New List(Of ChucVuDTO)
        ds1 = chucvu.laychucvu()
        ComboBox_chucvu.DataSource = ds1
        ComboBox_chucvu.DisplayMember = "TenChucVu"
        ComboBox_chucvu.ValueMember = "MaChucVu"


        '''''load  BoPhan len combox''''''''''''''

        Dim bophan As New BoPhanBUS()
        Dim ds2 As New List(Of BoPhanDTOvb)
        ds2 = bophan.laybophan()
        ComboBox_bophan.DataSource = ds2
        ComboBox_bophan.DisplayMember = "TenBoPhan"
        ComboBox_bophan.ValueMember = "MaBoPhan"
        ComboBox_bophan.SelectedIndex = 0


        LoadData()
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Close()

    End Sub

    Private Sub btnThem_hsnv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem_hsnv.Click
        Dim nvDTO As New NhanVienDTO()
        'lay du lieu tu cac control 
        nvDTO.HoTenNV = txthovaten_hsnv.Text.Trim()
        nvDTO.MaBangCap = ComboBox_bangcap.SelectedValue 'ComboBox_bangcap.Items(ComboBox_bangcap.SelectedIndex) 
        nvDTO.MaBoPhan = Convert.ToInt32(ComboBox_bophan.SelectedValue)
        nvDTO.MaChucVu = Convert.ToInt32(ComboBox_chucvu.SelectedValue)
        nvDTO.DiaChi = txtdiachi_hsnv.Text.Trim()
        nvDTO.DienThoai = txtdienthoai_hsnv.Text.Trim()
        nvDTO.NgaySinh = DateTimePicker_hsnv.Value.ToLongDateString()
        'Dim strSQL As String = "insert into NhanVien(HoTen,MaBangCap,MaBoPhan,MaChucVu,DiaChi,DienThoai,NgaySinh) values('" + nvDTO.HoTen.ToString() + "'," + nvDTO.MaBangCap.ToString() + "," + nvDTO.MaBoPhan.ToString() + "," + nvDTO.MaChucVu.ToString() + ",'" + nvDTO.DiaChi.ToString() + "','" + nvDTO.DienThoai.ToString() + "','" + nvDTO.NgaySinh.ToString() + "')"
        ''goi BUS
        Dim kq As Integer
        Dim bus As New NhanVienBUS()
        kq = bus.ThemNhanNhanVien(nvDTO)
        LoadData()
        If kq = 1 Then
            MessageBox.Show("them thanh cong")
            LoadData()
        Else
            MessageBox.Show("them that bai")
        End If

    End Sub

    Private Sub btnxoa_hsnv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnxoa_hsnv.Click
        Dim nvDTO As New NhanVienDTO()
        'lay du lieu tu cac control 
        Dim dr As DialogResult = New DialogResult()
        dr = MessageBox.Show("Ban muon xoa?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2)

        If dr = Windows.Forms.DialogResult.Yes Then
            'nvDTO.HoTen = txthovaten_hsnv.Text.Trim()
            'nvDTO.MaBangCap = ComboBox_bangcap.Text.Trim()
            'nvDTO.MaBoPhan = ComboBox_bophan.Text.Trim()
            'nvDTO.MaChucVu = ComboBox_chucvu.Text.Trim()
            'nvDTO.DiaChi = txtdiachi_hsnv.Text.Trim()
            'nvDTO.DienThoai = txtdienthoai_hsnv.Text.Trim()
            'nvDTO.NgaySinh = DateTimePicker_hsnv.Text.Trim()
            'goi BUS
            Dim manv As Int32

            If DataGridView_hosonhanvien.CurrentRow.Cells("ColMaNV").Value.ToString() <> "" Then
                manv = Convert.ToInt32(DataGridView_hosonhanvien.CurrentRow.Cells("ColMaNV").Value.ToString())
            End If

            Dim kq As Boolean
            Dim bus As New NhanVienBUS()
            kq = bus.xoanhanvien(manv)
            If kq = True Then
                MessageBox.Show("xoa thanh cong")
                LoadData()
            Else
                MessageBox.Show("xoa that bai")
            End If
        End If
    End Sub

    Private Sub btnsua_hsnv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsua_hsnv.Click
        Dim nvDTO As New NhanVienDTO()
        'lay du lieu tu cac control 
        nvDTO.HoTenNV = txthovaten_hsnv.Text.Trim()
        nvDTO.MaBangCap = ComboBox_bangcap.SelectedValue 'ComboBox_bangcap.Items(ComboBox_bangcap.SelectedIndex) 
        nvDTO.MaBoPhan = Convert.ToInt32(ComboBox_bophan.SelectedValue)
        nvDTO.MaChucVu = Convert.ToInt32(ComboBox_chucvu.SelectedValue)
        nvDTO.DiaChi = txtdiachi_hsnv.Text.Trim()
        nvDTO.DienThoai = txtdienthoai_hsnv.Text.Trim()
        nvDTO.NgaySinh = DateTimePicker_hsnv.Value.ToLongDateString()
        'goi(bus)

        Dim kq As Integer
        Dim bus As New NhanVienBUS()
        kq = bus.update(nvDTO)
    End Sub
End Class